-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Scanner to re.pyi #3071
Added Scanner to re.pyi #3071
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, left a few comments below.
@@ -153,3 +153,11 @@ def escape(string: AnyStr) -> AnyStr: ... | |||
|
|||
def purge() -> None: ... | |||
def template(pattern: Union[AnyStr, Pattern[AnyStr]], flags: _FlagsType = ...) -> Pattern[AnyStr]: ... | |||
|
|||
class Scanner: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add an undocumented flag. Also, this needs to be generic over AnyStr
for the methods to have correct types:
class Scanner: | |
# undocumented | |
class Scanner(Generic[AnyStr]): |
class Scanner: | ||
def __init__( | ||
self, | ||
lexicon: Sequence[Tuple[AnyStr, Callable[[Scanner, AnyStr], Any]]], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the action can be anything, not just a callable:
lexicon: Sequence[Tuple[AnyStr, Callable[[Scanner, AnyStr], Any]]], | |
lexicon: Sequence[Tuple[AnyStr, Any]], |
Alternatively, another TypeVar
for the return value might make sense, in which case this would be:
lexicon: Sequence[Tuple[AnyStr, Callable[[Scanner, AnyStr], Any]]], | |
lexicon: Sequence[Tuple[AnyStr, Union[None, _T, Callable[[Scanner, AnyStr], Optional[_T]]]], |
lexicon: Sequence[Tuple[AnyStr, Callable[[Scanner, AnyStr], Any]]], | ||
flags: _FlagsType = ..., | ||
) -> None: ... | ||
def scan(self, string: AnyStr) -> Tuple[List[Any], AnyStr]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the case of introducing another type var, List[Any]
-> List[_T]
.
Would this make sense? # undocumented
_T = TypeVar('_T', bound=AnyStr)
_T2 = TypeVar('_T2')
class Scanner(Generic[_T, _T2]):
def __init__(
self,
lexicon: Sequence[Tuple[_T, Union[None, _T2, Callable[[Scanner, _T], Optional[_T2]]]]],
flags: _FlagsType = ...,
) -> None: ...
def scan(self, string: _T) -> Tuple[List[_T2], _T]: ... Should we break it down in smaller pieces? |
Looks good with two changes: The |
So, something like this would do it? _T = TypeVar('_T')
# undocumented
class Scanner(Generic[_T]):
def __init__(
self,
lexicon: Sequence[Tuple[AnyStr, Union[None, _T, Callable[[Scanner, AnyStr], Optional[_T]]]]],
flags: _FlagsType = ...,
) -> None: ...
def scan(self, string: AnyStr) -> Tuple[List[_T], AnyStr]: ... What is the reason that |
In this case you have a generic method on a generic class. The class is generic over _T, and the method is generic over AnyStr. So for example, if you have a |
Also, flake8 fails because of white space in line 156. @jonathanslenders Are you still interested in this PR? |
Closing for now, please reopen if you find time to work on this. |
No description provided.